﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ict.DataAccess
{
    /// <summary>
    /// 用于格式化保存查询的排序方式
    /// </summary>
    public class Sort
    {
        /// <summary>
        /// 排序方向枚举
        /// </summary>
        public enum SortDirection
        {
            /// <summary>
            /// 升序排序
            /// </summary>
            Ascending,
            /// <summary>
            /// 降序排序
            /// </summary>
            Descending
        }

        private string _fieldName = null;
        public string FieldName
        {
            get
            {
                return _fieldName;
            }
        }

        private SortDirection _direction = SortDirection.Ascending;

        public SortDirection Direction
        {
            get
            {
                return _direction;
            }
        }

        public Sort(string fieldName)
        {
            _fieldName = fieldName;
        }

        public Sort(string fieldName, SortDirection direction)
        {
            _fieldName = fieldName;
            _direction = direction;
        }

        public override string ToString()
        {
            return _fieldName + " " + ((_direction == SortDirection.Descending) ? "DESC" : "ASC");
        }

        /// <summary>
        /// 生成正序排列对象
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <returns>正序排列对象</returns>
        public static Sort Asc(string fieldName)
        {
            return new Sort(fieldName);
        }

        /// <summary>
        /// 生成反序排列对象
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <returns>反序排列对象</returns>
        public static Sort Desc(string fieldName)
        {
            return new Sort(fieldName, SortDirection.Descending);
        }
    }
}
